home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / DistUpgrade / MetaRelease.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  7.5 KB  |  256 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import thread
  5. import urllib2
  6. import httplib
  7. import os
  8. import string
  9. import apt_pkg
  10. import time
  11. import sys
  12. import rfc822
  13. from ConfigParser import ConfigParser
  14. from subprocess import Popen, PIPE
  15. from utils import *
  16.  
  17. class Dist(object):
  18.     
  19.     def __init__(self, name, version, date, supported):
  20.         self.name = name
  21.         self.version = version
  22.         self.date = date
  23.         self.supported = supported
  24.         self.releaseNotesURI = None
  25.         self.upgradeTool = None
  26.         self.upgradeToolSig = None
  27.  
  28.  
  29.  
  30. class MetaReleaseCore(object):
  31.     '''
  32.     A MetaReleaseCore object astracts the list of released 
  33.     distributions. 
  34.     '''
  35.     DEBUG = 'DEBUG_UPDATE_MANAGER' in os.environ
  36.     CONF = '/etc/update-manager/release-upgrades'
  37.     CONF_METARELEASE = '/etc/update-manager/meta-release'
  38.     
  39.     def __init__(self, useDevelopmentRelease = False, useProposed = False, forceLTS = False):
  40.         self._debug('MetaRelease.__init__() useDevel=%s useProposed=%s' % (useDevelopmentRelease, useProposed))
  41.         self.downloading = True
  42.         self.new_dist = None
  43.         self.current_dist_name = get_dist()
  44.         self.no_longer_supported = None
  45.         self.METARELEASE_URI = 'http://changelogs.ubuntu.com/meta-release'
  46.         self.METARELEASE_URI_LTS = 'http://changelogs.ubuntu.com/meta-release-lts'
  47.         self.METARELEASE_URI_UNSTABLE_POSTFIX = '-development'
  48.         self.METARELEASE_URI_PROPOSED_POSTFIX = '-development'
  49.         parser = ConfigParser()
  50.         if os.path.exists(self.CONF_METARELEASE):
  51.             parser.read(self.CONF_METARELEASE)
  52.             if parser.has_section('METARELEASE'):
  53.                 sec = 'METARELEASE'
  54.                 for k in [
  55.                     'URI',
  56.                     'URI_LTS',
  57.                     'URI_UNSTABLE_POSTFIX',
  58.                     'URI_PROPOSED_POSTFIX']:
  59.                     if parser.has_option(sec, k):
  60.                         self._debug('%s: %s ' % (self.CONF_METARELEASE, parser.get(sec, k)))
  61.                         setattr(self, '%s_%s' % (sec, k), parser.get(sec, k))
  62.                         continue
  63.                 
  64.             
  65.         
  66.         parser = ConfigParser()
  67.         if os.path.exists(self.CONF):
  68.             parser.read(self.CONF)
  69.             if parser.has_option('DEFAULT', 'Prompt'):
  70.                 type = parser.get('DEFAULT', 'Prompt').lower()
  71.                 if type == 'never' or type == 'no':
  72.                     self.downloading = False
  73.                     return None
  74.                 if type == 'lts':
  75.                     self.METARELEASE_URI = self.METARELEASE_URI_LTS
  76.                 
  77.             
  78.         
  79.         if forceLTS:
  80.             self.METARELEASE_URI = self.METARELEASE_URI_LTS
  81.         
  82.         if useDevelopmentRelease:
  83.             self.METARELEASE_URI += self.METARELEASE_URI_UNSTABLE_POSTFIX
  84.         elif useProposed:
  85.             self.METARELEASE_URI += self.METARELEASE_URI_PROPOSED_POSTFIX
  86.         
  87.         self._debug('metarelease-uri: %s' % self.METARELEASE_URI)
  88.         self.metarelease_information = None
  89.         if not self._buildMetaReleaseFile():
  90.             self._debug('_buildMetaReleaseFile failed')
  91.             return None
  92.         t = thread.start_new_thread(self.download, ())
  93.  
  94.     
  95.     def _buildMetaReleaseFile(self):
  96.         self.METARELEASE_FILE = os.path.join('/var/lib/update-manager/', os.path.basename(self.METARELEASE_URI))
  97.         
  98.         try:
  99.             open(self.METARELEASE_FILE, 'a')
  100.         except IOError:
  101.             e = None
  102.             path = os.path.expanduser('~/.update-manager-core/')
  103.             if not os.path.exists(path):
  104.                 
  105.                 try:
  106.                     os.mkdir(path)
  107.                 except OSError:
  108.                     e = None
  109.                     sys.stderr.write("mkdir() failed: '%s'" % e)
  110.                     return False
  111.                 
  112.  
  113.             None<EXCEPTION MATCH>OSError
  114.             self.METARELEASE_FILE = os.path.join(path, os.path.basename(self.METARELEASE_URI))
  115.  
  116.         
  117.         try:
  118.             if os.path.getsize(self.METARELEASE_FILE) == 0:
  119.                 os.unlink(self.METARELEASE_FILE)
  120.         except Exception:
  121.             e = None
  122.  
  123.         return True
  124.  
  125.     
  126.     def dist_no_longer_supported(self, dist):
  127.         ''' virtual function that is called when the distro is no longer
  128.             supported
  129.         '''
  130.         self.no_longer_supported = dist
  131.  
  132.     
  133.     def new_dist_available(self, dist):
  134.         ''' virtual function that is called when a new distro release
  135.             is available
  136.         '''
  137.         self.new_dist = dist
  138.  
  139.     
  140.     def parse(self):
  141.         self._debug('MetaRelease.parse()')
  142.         current_dist_name = self.current_dist_name
  143.         self._debug("current dist name: '%s'" % current_dist_name)
  144.         current_dist = None
  145.         dists = []
  146.         index_tag = apt_pkg.ParseTagFile(self.metarelease_information)
  147.         step_result = index_tag.Step()
  148.         while step_result:
  149.             if index_tag.Section.has_key('Dist'):
  150.                 name = index_tag.Section['Dist']
  151.                 rawdate = index_tag.Section['Date']
  152.                 date = time.mktime(rfc822.parsedate(rawdate))
  153.                 supported = int(index_tag.Section['Supported'])
  154.                 version = index_tag.Section['Version']
  155.                 dist = Dist(name, version, date, supported)
  156.                 if index_tag.Section.has_key('ReleaseNotes'):
  157.                     dist.releaseNotesURI = index_tag.Section['ReleaseNotes']
  158.                 
  159.                 if index_tag.Section.has_key('UpgradeTool'):
  160.                     dist.upgradeTool = index_tag.Section['UpgradeTool']
  161.                 
  162.                 if index_tag.Section.has_key('UpgradeToolSignature'):
  163.                     dist.upgradeToolSig = index_tag.Section['UpgradeToolSignature']
  164.                 
  165.                 dists.append(dist)
  166.                 if name == current_dist_name:
  167.                     current_dist = dist
  168.                 
  169.             
  170.             step_result = index_tag.Step()
  171.         if current_dist is None:
  172.             return False
  173.         upgradable_to = ''
  174.         for dist in dists:
  175.             if dist.date > current_dist.date and dist.supported == True:
  176.                 upgradable_to = dist
  177.                 self._debug('new dist: %s' % upgradable_to)
  178.                 break
  179.                 continue
  180.             current_dist is None
  181.         
  182.         if upgradable_to != '' and not (current_dist.supported):
  183.             self.dist_no_longer_supported(upgradable_to)
  184.         
  185.         if upgradable_to != '':
  186.             self.new_dist_available(upgradable_to)
  187.         
  188.         return True
  189.  
  190.     
  191.     def download(self):
  192.         self._debug('MetaRelease.download()')
  193.         lastmodified = 0
  194.         req = urllib2.Request(self.METARELEASE_URI)
  195.         req.add_header('Cache-Control', 'No-Cache')
  196.         req.add_header('Pragma', 'no-cache')
  197.         if os.access(self.METARELEASE_FILE, os.W_OK):
  198.             lastmodified = os.stat(self.METARELEASE_FILE).st_mtime
  199.         
  200.         if lastmodified > 0:
  201.             req.add_header('If-Modified-Since', time.asctime(time.gmtime(lastmodified)))
  202.         
  203.         
  204.         try:
  205.             uri = urllib2.urlopen(req)
  206.             if os.path.exists(self.METARELEASE_FILE) and not os.access(self.METARELEASE_FILE, os.W_OK):
  207.                 
  208.                 try:
  209.                     os.unlink(self.METARELEASE_FILE)
  210.                 except OSError:
  211.                     e = None
  212.                     print "Can't unlink '%s' (%s)" % (self.METARELEASE_FILE, e)
  213.                 except:
  214.                     None<EXCEPTION MATCH>OSError
  215.                 
  216.  
  217.             None<EXCEPTION MATCH>OSError
  218.             
  219.             try:
  220.                 f = open(self.METARELEASE_FILE, 'w+')
  221.                 for line in uri.readlines():
  222.                     f.write(line)
  223.                 
  224.                 f.flush()
  225.                 f.seek(0, 0)
  226.                 self.metarelease_information = f
  227.             except IOError:
  228.                 e = None
  229.  
  230.             uri.close()
  231.         except (urllib2.URLError, httplib.BadStatusLine):
  232.             e = None
  233.             if os.path.exists(self.METARELEASE_FILE):
  234.                 self.metarelease_information = open(self.METARELEASE_FILE, 'r')
  235.             
  236.         except:
  237.             os.path.exists(self.METARELEASE_FILE)
  238.  
  239.         if self.metarelease_information != None:
  240.             self._debug('have self.metarelease_information')
  241.             self.parse()
  242.         else:
  243.             self._debug('NO self.metarelease_information')
  244.         self.downloading = False
  245.  
  246.     
  247.     def _debug(self, msg):
  248.         if self.DEBUG:
  249.             sys.stderr.write(msg + '\n')
  250.         
  251.  
  252.  
  253. if __name__ == '__main__':
  254.     meta = MetaReleaseCore(False, False)
  255.  
  256.